package com.lovo.bbs.filter;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.UserInfoBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.LoginVo;
import com.lovo.bbs.vo.UserInfoVo;

/**
 * 打开用户信息页面时,初始化用户信息
 * 
 * @author tiancen2001
 * 
 */
public class InitUserInfoFilter implements Filter {

	public void destroy() {

	}

	@SuppressWarnings("unchecked")
	public void doFilter(ServletRequest servletrequest,
			ServletResponse servletresponse, FilterChain filterchain)
			throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) servletrequest;
		String userName = req.getParameter("username");

		// 取得用户信息
		UserInfoVo userInfo = null;
		userInfo = new UserInfoBo().getUserInfo(userName,UserInfoBo.TYPE_OF_MEMBER);

		// 取得在线用户数据
		ServletContext contex = req.getSession().getServletContext();
		HashMap<String, LoginVo> onlineUser = (HashMap<String, LoginVo>) contex
				.getAttribute("onlineUser");
		// 设置在线状态
		if (onlineUser.containsKey(userInfo.getUserName())) {
			userInfo.setOnlineState("在线");
		} else {
			userInfo.setOnlineState("离线");
		}
		req.setAttribute("userInfo", userInfo);

		// 取得网站杂项信息
		ServletContext context = req.getSession().getServletContext();
		ForumStatVo forumInfo = (ForumStatVo) context.getAttribute("forumInfo");
		// 更新网站杂项信息
		if (forumInfo == null) {
			forumInfo =new ForumStatBo().getForumInfo();
		}
		context.setAttribute("forumInfo", forumInfo);
		req.setAttribute("forumInfo", forumInfo);

		filterchain.doFilter(servletrequest, servletresponse);
	}

	public void init(FilterConfig filterconfig) throws ServletException {

	}

}
